<?php

//阿里云oss上传

namespace App\Utils\Oss;
use OSS\OssClient;
use OSS\Core\OssException;
use App\Exception\ValidateException;

class AliOssService
{
	
	/**
	 * 阿里云oss上传
	 * @param  array $tempFile 本地图片路径
	 * @return string 图片上传返回的url地址
	 */
	public static function upload($file,$filepath){
		try {
			$isCName = strpos(config('my.ali_oss_endpoint'),'aliyuncs.com') > 0 ? false : true;
			$ossClient = new OssClient(config('my.ali_oss_accessKeyId'), config('my.ali_oss_accessKeySecret'),config('my.ali_oss_endpoint'),$isCName);
			$result = $ossClient->putObject(config('my.ali_oss_bucket'),$filepath,file_get_contents($file->getRealPath()));
		} catch (OssException $e) {
			logger()->error('阿里oss错误：'.print_r($e->getMessage(),true));
			throw new ValidateException('上传失败');
		}
		return $result['info']['url'];
	}
	
}
